/*
 * Created on 2004-8-8 UNIONMON��Դ����ϵͳ @author {author} package com.phevos.common.dbpool
 */

package com.phevos.common.dbpool;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 * ��ȡl�ӳ�������Ϣ�ࡣ
 */
public class PoolConfig extends Object
{

    static PoolConfig instance = null;

    public static synchronized PoolConfig getInstance(String configFile)
    {
        if (instance == null)
        {
            instance = new PoolConfig(configFile);
        }
        return instance;
    }

    public static synchronized PoolConfig getInstance()
    {
        return getInstance(PROPFILE);
    }

    /**
     * Automatically generated constructor: PoolConfig
     */
    private PoolConfig(String confFile)
    {
        init(confFile);
    }

    private Properties prop = new Properties();

    private java.util.Hashtable poolSettings = new java.util.Hashtable();

    private static String PROPFILE = "conf/db.properties";

    private static final String POOL_DATABASE_URL = "pool.database.url";

    private static final String POOL_DATABASE_DRIVER = "pool.database.driver";

    private static final String POOL_DATABASE_USER = "pool.database.user";

    private static final String POOL_DATABASE_PASSWORD = "pool.database.password";

    private static final String POOL_POOLNAMES = "pool.poolnames";

    public void setPROPFILE(String filename)
    {
        PROPFILE = filename;
        if (PROPFILE != null)
        {
            init(filename);
        }
    }

    /**
     * ���ز���ʼ��l�ӳ������ļ���
     */
    private final synchronized void init(String filename)
    {
        FileInputStream configStream = null;
        try
        {
            File configFile = new File(filename);
            configStream = new FileInputStream(configFile);
            prop.load(configStream);
            // ��ʼ��PoolSetting
            String poolnames = getProperty(POOL_POOLNAMES);
            String poolname = null;
            if (poolnames != null && !poolnames.trim().equals(""))
            {
                StringTokenizer st = new StringTokenizer(poolnames, ",");
                while (st.hasMoreTokens())
                {
                    poolname = st.nextToken();
                    if (poolname != null && !poolname.trim().equals(""))
                    {
                        PoolSetting poolSetting = new PoolSetting();
                        poolSetting.init(poolname, prop);
                        poolSettings.put(poolname, poolSetting);
                    }
                }
            }
            configStream.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            if (configStream != null)
            {
                try
                {
                    configStream.close();
                }
                catch (Throwable t)
                {
                    System.out.println("failed close configStream");
                }
            }
        }
    }

    /**
     * �������ļ��ж�ȡ�����ļ���Ϣ��
     */
    final String getProperty(String name)
    {
        return prop.getProperty(name);
    }

    // /////////////////////global setting for db pool///////////////////
    public String getUrl()
    {
        return PoolConfig.getInstance().getProperty(POOL_DATABASE_URL);
    }

    public String getDriver()
    {
        return PoolConfig.getInstance().getProperty(POOL_DATABASE_DRIVER);
    }

    public String getUser()
    {
        return PoolConfig.getInstance().getProperty(POOL_DATABASE_USER);
    }

    public String getPassword()
    {
        return PoolConfig.getInstance().getProperty(POOL_DATABASE_PASSWORD);
    }

    public PoolSetting getPoolSetting(String poolname)
    {
        return (PoolSetting)poolSettings.get(poolname);
    }

    // /////////////////////end global setting for db pool///////////////////

    public static void main(String[] args)
    {
        PoolSetting poolsetting = PoolConfig.getInstance().getPoolSetting("basicpool");
        System.out.println(poolsetting);
        poolsetting = PoolConfig.getInstance().getPoolSetting("querypool");
        System.out.println(poolsetting);
    }

}

/**
 * ÿ��db pool��������Ϣ
 * 
 * @author zhaoch
 * @version 1.0
 */
class PoolSetting
{
    String poolname = null;

    int maxActive = 8;

    int maxIdle = 8;

    int minIdle = 0;

    long maxWait = -1;

    long timeBetweenEvictionRunsMillis = -1;

    int numTestsPerEvictionRun = 3;

    long minEvictableIdleTimeMillis = 1800000L;

    void init(String argpoolname, Properties prop)
    {
        poolname = argpoolname;
        maxActive = this.retriveValue("maxActive", maxActive, prop);
        maxIdle = this.retriveValue("maxIdle", maxIdle, prop);
        maxWait = this.retriveValue("maxWait", maxWait, prop);
        minEvictableIdleTimeMillis = this.retriveValue("minEvictableIdleTimeMillis",
                minEvictableIdleTimeMillis, prop);
        minIdle = this.retriveValue("minIdle", minIdle, prop);
        timeBetweenEvictionRunsMillis = this.retriveValue("timeBetweenEvictionRunsMillis",
                timeBetweenEvictionRunsMillis, prop);
        numTestsPerEvictionRun = this.retriveValue("numTestsPerEvictionRun",
                numTestsPerEvictionRun, prop);
    }

    public int getMaxActive()
    {
        return maxActive;
    }

    public int getMaxIdle()
    {
        return maxIdle;
    }

    public long getMaxWait()
    {
        return maxWait;
    }

    public long getMinEvictableIdleTimeMillis()
    {
        return minEvictableIdleTimeMillis;
    }

    public int getMinIdle()
    {
        return minIdle;
    }

    public int getNumTestsPerEvictionRun()
    {
        return numTestsPerEvictionRun;
    }

    public String getPoolname()
    {
        return poolname;
    }

    public long getTimeBetweenEvictionRunsMillis()
    {
        return timeBetweenEvictionRunsMillis;
    }

    public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
    {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    public void setPoolname(String poolname)
    {
        this.poolname = poolname;
    }

    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
    {
        this.numTestsPerEvictionRun = numTestsPerEvictionRun;
    }

    public void setMinIdle(int minIdle)
    {
        this.minIdle = minIdle;
    }

    public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
    {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public void setMaxWait(long maxWait)
    {
        this.maxWait = maxWait;
    }

    public void setMaxIdle(int maxIdle)
    {
        this.maxIdle = maxIdle;
    }

    public void setMaxActive(int maxActive)
    {
        this.maxActive = maxActive;
    }

    int retriveValue(String argkey, int defaultValue, Properties prop)
    {
        String key = poolname + "." + argkey;
        String str = prop.getProperty(key);
        if (str != null && !str.trim().equals(""))
        {
            try
            {
                return Integer.parseInt(str);
            }
            catch (Throwable t)
            {
            }
        }
        return defaultValue;

    }

    long retriveValue(String argkey, long defaultValue, Properties prop)
    {
        String key = poolname + "." + argkey;
        String str = prop.getProperty(key);
        if (str != null && !str.trim().equals(""))
        {
            try
            {
                return Long.parseLong(str);
            }
            catch (Throwable t)
            {
            }
        }
        return defaultValue;

    }

}
